import axios from 'axios'
import store from '@/global/store.js'
import jsonFormat from 'json-format'

const common = {}

/**
 * zh_CN --> zh-cn
 */
common.convertLocale = function(locale){
  locale = locale.replace('_', '-');
  locale = locale.toLowerCase();
  return locale;
}

/**
 * viewName 页面名称
 * successCallback 成功后调用
 * failureCallback 失败后调用
 */
common.sessionCheck = function(viewName, successCallback, failureCallback){
  axios({
    method: 'get',
    url: '/infra/security/sessionCheck',
    params: {viewName: viewName},
  }).then(()=>{
    if(successCallback){
      successCallback()
    }
  }).catch((error)=>{
    if(failureCallback){
      failureCallback(error)
    }
  })
}

common.getVisibleFunc = function(menuCode) {
  let result = {}

  let filteredData = store.state.visibleFunc.filter(item => {
    return item.menuCode == menuCode
  })
  
  filteredData.forEach(item => {
    let key = item.funcCode
    result[key]= item.funcNameI18n
  })

  return result
}

common.getDictByDictCodeItemCode = function(dictCode, itemCode) {
  let result = ''
  const dicts = store.state.dicts[dictCode] || []
  for (let i=0 ; i< dicts.length; i ++) {
    let dict = dicts[i]
    if (dict.itemCode == itemCode ) {
      result = dict
      break
    }
  }

  return result
}

common.getItemNameI18n = function(dictCode, itemCode) {
  let dict = this.getDictByDictCodeItemCode(dictCode, itemCode) || {}
  let itemNameI18n = dict.itemNameI18n
  if (itemNameI18n) {
    return itemNameI18n[localStorage.locale]
  }
  return dict.itemName
}

common.sorterDictItemCodes = function(dictCode, itemCodes) {
  let result = []
  const dicts = store.state.dicts[dictCode] || []
  for (let i=0 ; i< dicts.length; i ++) {
    let dict = dicts[i];
    if (itemCodes.includes(dict.itemCode)) {
      result.push(dict.itemCode)
    }
  }

  return result
}

common.getPagination = function() {
  return {
    current: 1,
    pageSize: 10,
    total: 0,
    showSizeChanger: true,
    showQuickJumper: true,
    pageSizeOptions: ['10','30','50']
  }
}

common.stringSort = function(a, b) {
  a = a || ''
  b = b || ''
  a = a.toUpperCase()
  b = b.toUpperCase()
  if (a < b) {
    return -1
  }
  if (a > b) {
    return 1
  }
  return 0
}

common.numberSort = function(a, b){
  a = a || 0
  b = b || 0
  return a - b
}

common.prettyJson = function(jsonStr) {
  jsonStr = jsonStr || ''
  let result = jsonStr.trim()
  if (jsonStr && (jsonStr.startsWith('{') || jsonStr.startsWith('['))) {
    try {
      let obj = JSON.parse(jsonStr)
      result = jsonFormat(obj)
    } catch(e) {
      console.log(e)
    }
  }
  return result
}

common.getCommaJoinTrimStr = function(str) {
  str = str || ''
  str = str.replace('，', ',')
  let arr = str.split(',')
  arr = arr.map(item =>{
    return item.trim()
  })
  return arr.join()
}

common.getJosnValue = function(obj, property) {
  let properties = property.split('.')
  let result = obj || {}
  for (let i = 0 ; i < properties.length ; i++) {
    let p = properties[i]
    if (i == properties.length - 1) {
      result = result[p]
    } else {
      result = result[p] || {}
    }
  }

  return result
},

common.getCommaJoinTrimSpaceStr = function(str) {
  if(!str){
    return ''
  }else{
    str=str.trim()
    str = str.split(/\n|,|，/g)
    str = str.filter(item => {
      return item != ''
    });
    return str.join()
  }
}

export default common
